package cn.com.yang.java8;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.function.Function;
import java.util.stream.Collectors;

import org.junit.Test;

/**
 * @Author 杨亿金
 * @Date 2022/3/14 22:20
 * @CopyRight
 * @Version
 */
public class GroupingBy {
    List<Item> items = Arrays.asList(
            new Item("apple", 10, new Integer(235)),
            new Item("apple", 20, new Integer(325)),
            new Item("orange", 30, new Integer(139)),
            new Item("orange", 20, new Integer(335)),
            new Item("orange", 10, new Integer(635)),
            new Item("orange", 50, new Integer(415)),
            new Item("peach", 20, new Integer(265)),
            new Item("peach", 30, new Integer(425)),
            new Item("peach", 40, new Integer(245)),
            new Item("peach", 10, new Integer(150)),
            new Item("orange", 10, new Integer(150)),
            new Item("orange", 10, new Integer(150))
    );
    /**
     * 分组计数
     */
    @Test
    public void test(){
        List<String> items = Arrays.asList("apple","apple","orange","blue","peach","peach");
        //分组计数
        Map<String,Long> result = items.stream()
                .collect(Collectors.groupingBy(Function.identity(),Collectors.counting()));
        System.out.println(result);
    }
    /**
     * 分组，计数，数量
     */
    @Test
    public void test1(){
        Map<String, Integer> sum = items.stream()
                .collect(Collectors.groupingBy(Item::getName, Collectors.summingInt(Item::getQty)));
        System.out.println(sum);
    }

    /**
     * 分组List
     */
    @Test
    public void test2(){
        Map<String,List<Item>> map = items.stream()
                .collect(Collectors.groupingBy(Item::getName));
        System.out.println(map);
    }
    @Test
    public void testGrop2(){
    	List<Map<String,String>> list = new ArrayList();
    	Map map1 = new HashMap();
    	map1.put("PD_TYPE", "0");
    	map1.put("TITLE", "xsadasdasd的");
    	list.add(map1);
    	Map map2 = new HashMap();
    	map2.put("PD_TYPE", "0");
    	map2.put("TITLE", "xs111adasdasd嘻嘻嘻");
    	list.add(map2);
    	Map map3 = new HashMap();
    	map3.put("PD_TYPE", "1");
    	map3.put("TITLE", "xs111ada444sdasd");
    	list.add(map3);
    	Map map4 = new HashMap();
    	map4.put("PD_TYPE", "1");
    	map4.put("TITLE", "你哈xs111ada444sdasd");
    	list.add(map4);
        Map<String,List<Map<String,String>>> map = list.stream()
                .collect(Collectors.groupingBy(m->(String)m.get("PD_TYPE")));
        System.out.println(map);
    }

    /**
     * 分组List去重
     *
     */
    @Test
    public void test3(){
        // 分组 转化List->Set
        Map<Integer, Set<String>> result = items.stream()
                .collect(Collectors.groupingBy(Item::getPrice, Collectors.mapping(Item::getName, Collectors.toSet())));
        System.out.println(result);
    }
    
    @Test
    public void testGrop4(){
    	List<Map<String,Object>> list = new ArrayList();
    	Map map1 = new HashMap();
    	map1.put("PD_TYPE", "0");
    	map1.put("TITLE", "xsadasdasd的");
    	list.add(map1);
    	Map map2 = new HashMap();
    	map2.put("PD_TYPE", "0");
    	map2.put("TITLE", "xs111adasdasd嘻嘻嘻");
    	list.add(map2);
    	Map map3 = new HashMap();
    	map3.put("PD_TYPE", "1");
    	map3.put("TITLE", "xs111ada444sdasd");
    	list.add(map3);
    	Map map4 = new HashMap();
    	map4.put("PD_TYPE", "1");
    	map4.put("TITLE", "你哈xs111ada444sdasd");
    	list.add(map4);
        Map<Object,List<Map<String,Object>>> map = list.stream()
                .collect(Collectors.groupingBy(m->m.get("PD_TYPE")));
//        Map<String,List<Map<String,String>>> map = list.stream()
//                .collect(Collectors.groupingBy(m->(String)m.get("PD_TYPE")));
        System.out.println(map);
    }
}
